package com.world.cat.service.sys;

import com.world.cat.dao.sys.UserDao;
import com.world.common.util.CollectionUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.persistence.Query;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by gang on 2017/6/8.
 */
@Service
@Transactional
public class AppService {
    @Resource
    private UserDao userDao;

    public Map<String, List<String>> statisticTable() {
        String sql = "select table_name,table_rows from information_schema.tables\n" +
                "where TABLE_SCHEMA = 'JAVA-CAT'\n" +
                "order by table_rows asc;";
        Query sqlQuery = userDao.createSQLQuery(sql);
        List results = sqlQuery.getResultList();

        List<String> xAxis = new ArrayList<>();
        List<String> yAxis = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(results)) {
            for (Object oArr : results) {
                Object[] arr = (Object[]) oArr;
                xAxis.add(arr[0].toString());
                yAxis.add(arr[1].toString());
            }
        }
        Map<String, List<String>> dataMap = new HashMap<>();
        dataMap.put("x", xAxis);
        dataMap.put("y", yAxis);
        return dataMap;
    }
}
